<?php

require_once ROOT_APPLICATION_MODULES_ADMIN_MODELS.DS."Menu.php";
require_once ROOT_APPLICATION_MODULES_ADMIN_MODELS.DS."MenuItem.php";


require_once ROOT_LIBRARY_UTILITY.DS."Converter.php";

class Application_Forms_Menu_Item extends Zend_Form
{
    public function init()
    {
        $this->setName('menu_item');

        /* Hiden field, some field use in future extend */
        
        $menu_item_id = new Zend_Form_Element_Hidden('menu_item_id');
        $item_id = new Zend_Form_Element_Hidden('item_id');
        $label = new Zend_Form_Element_Text('label');
        $label->setLabel("Tên");
        $link = new Zend_Form_Element_Text('link');
        $link->setLabel("Đường dẫn");
        $parent_id = new Zend_Form_Element_Hidden('parent_id');
        
        /* Visible field */
        
		$menu_id = new Zend_Form_Element_Select('menu_id');
        $menu_id->setLabel('Belong to menu');
        
        $menu_model = new End_Models_Menu();
        $menus = $menu_model->fetchAll();
        foreach($menus as $c) {
        	$menu_id->addMultiOption($c->menu_id,
        		$c->name);
        }
        //
        // for left_id
        //
        $left_id = new Zend_Form_Element_Select('left_id');
        $left_id->setLabel("Menu bên trái");
        $menu_item_model = new Application_Models_Menu_Item();
        $menu_items = $menu_item_model->fetchAll();
        foreach($menu_items as $c) {
        	$left_id->addMultiOption($c->menu_item_id,
        		$c->label);
        }
        //
        // for right_id
        //
        $right_id = new Zend_Form_Element_Select('right_id');
        $right_id->setLabel("Menu bên phải");
        foreach($menu_items as $c) {
        	$right_id->addMultiOption($c->menu_item_id,
        		$c->label);
        	
        }
        
        /* Submit button */
        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setAttrib('id', 'submitbutton');
        
        /* add component to current form */

        $this->addElements(array($menu_item_id, $item_id, 
        		$label, $link, $parent_id, $menu_id, $left_id, $right_id, $submit));
    }
        	//if($c->menu_item_id != $this->getValue('menu_item_id'))
    public function populate($values)
    {
    	$id = $values['menu_item_id'];
    	$label = $values['label'];
    	$this->left_id->removeMultiOption($id, $label);
    	$this->right_id->removeMultiOption($id, $label);
    	parent::populate($values);
    }   	
}